package com.jf.mall.web;

import com.jf.mall.domain.Furniture;
import com.jf.mall.domain.Page;
import com.jf.mall.service.FurnitureService;
import com.jf.mall.utils.DataUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-09-30
 * Time:1:04
 */
@WebServlet(name = "CustomerServlet", urlPatterns = "/customerServlet")
public class CustomerServlet extends BasicServlet {
    private FurnitureService furnitureService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        furnitureService = new FurnitureService();
    }

    private void pageByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = DataUtils.parseInt(req.getParameter("page"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.DEFAULT_PAGE_SIZE);
        String name = req.getParameter("name");
        name = name == null ? "" : name;
        Page<Furniture> page = furnitureService.getFurniturePageByName(pageNo, pageSize, name);
        page.setUrl(req.getServletContext().getContextPath() + req.getServletPath() + "?action=pageByName&name=" + name);
        req.setAttribute("page", page);
        req.getRequestDispatcher("/views/customer/index.jsp").forward(req, resp);
    }
}
